package com.zl_huang.example.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;

import com.zl_huang.example.model.ExamTestType;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.Test;
import com.zl_huang.example.model.vo.CommonMessageResultVO;
import com.zl_huang.example.model.vo.ExamTestTypeVO;
import com.zl_huang.example.model.vo.SubjectVO;
import com.zl_huang.example.model.vo.TestVO;
import com.zl_huang.example.service.IExamTestTypeService;
import com.zl_huang.example.service.ISubjectService;
import com.zl_huang.example.service.ITestService;
@Controller
public class TestController {
	@Autowired
	private ITestService testService;
	@Autowired
	private LocaleResolver localeResolver;
	@Autowired
	private ISubjectService subjectService;
	@Autowired
	private IExamTestTypeService examTestTypeService;
	@RequestMapping("/helloword")
	public String helloWord(Model model){
		model.addAttribute("data", "hello word");
		return "test/TestOne";
	}
	@RequestMapping("/testList")
	public String toTestPage(Model model){
		List<Test> testList;
		try {
			 testList = testService.findAllTest();
			model.addAttribute("testList",testList);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "test/TestList";
	}
	@ResponseBody
	@RequestMapping(value="/getTestById",method = RequestMethod.POST)
	public TestVO getTestById(Integer testId){
		TestVO result = null;
		try {
		    result = testService.getTestVOById(testId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/getSubjectAndTestType",method = RequestMethod.POST)
	public Map<String ,Object> getSubjectAndTestType(){
		Map<String , Object> resultMap = new HashMap<String , Object>();
		try {
			List<Subject> subjcetList = subjectService.findAllSubject();
			List<SubjectVO> subjectVOList = new ArrayList<SubjectVO>();
			for(Subject subject : subjcetList){
				SubjectVO subjectVO = new SubjectVO();
				BeanUtils.copyProperties(subject, subjectVO);
				subjectVOList.add(subjectVO);
			}
			resultMap.put("subjectVOList", subjectVOList);
			List<ExamTestTypeVO> examTestTypeVOList = examTestTypeService.findAllExamTestTypet();
			resultMap.put("examTestTypeVOList", examTestTypeVOList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultMap;
		
	}
	@ResponseBody
	@RequestMapping(value="/saveTest")
	public CommonMessageResultVO saveTest(Integer sub_id,Integer testType,String content,
			String chooseA,String chooseB,String chooseC,String chooseD,String answerString){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String ,String> messageMap = new HashMap<String ,String>();
		try {
			Test test = new Test();
			Subject subject = subjectService.getSubjectById(sub_id);
			if(subject != null){
				test.setSubject(subject);
			}
			ExamTestType examTestType = examTestTypeService.getExamTestTypeById(testType);
			if(examTestType != null){
				test.setExamTestType(examTestType);
			}
			test.setContent(content);
			test.setChooseA(chooseA);
			test.setChooseB(chooseB);
			test.setChooseC(chooseC);
			test.setChooseD(chooseD);
			test.setAnswer(answerString.trim());
			Integer flag = testService.createTest(test);
			if(flag == 1){
				resultVO.setSuccess(true);
			}else{
				resultVO.setSuccess(false);
			}
		} catch (Exception e) {
			
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
		
	}
	
	@ResponseBody
	@RequestMapping(value="/toEditTest",method = RequestMethod.POST)
	public Map<String ,Object> toEditTest(Integer testId){
		Map<String ,Object> resultMap = new HashMap<String ,Object>();
		try {
			TestVO testVO = testService.getTestVOById(testId);
			if(testVO != null){
				resultMap.put("testVO", testVO);
			}
			
			List<Subject> subjcetList = subjectService.findAllSubject();
			List<SubjectVO> subjectVOList = new ArrayList<SubjectVO>();
			for(Subject subject : subjcetList){
				SubjectVO subjectVO = new SubjectVO();
				BeanUtils.copyProperties(subject, subjectVO);
				subjectVOList.add(subjectVO);
			}
			resultMap.put("subjectVOList", subjectVOList);
			List<ExamTestTypeVO> examTestTypeVOList = examTestTypeService.findAllExamTestTypet();
			resultMap.put("examTestTypeVOList", examTestTypeVOList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultMap;
		
	}
	
	@ResponseBody
	@RequestMapping(value="/editTest")
	public CommonMessageResultVO editTest(Integer testId ,Integer sub_id,Integer testType,String content,
			String chooseA,String chooseB,String chooseC,String chooseD,String answerString){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String ,String> messageMap = new HashMap<String ,String>();
		try {
			Test test = testService.getTestById(testId);
			if(test != null){
				Subject subject = subjectService.getSubjectById(sub_id);
				if(subject != null){
					test.setSubject(subject);
				}
				ExamTestType examTestType = examTestTypeService.getExamTestTypeById(testType);
				if(examTestType != null){
					test.setExamTestType(examTestType);
				}
				test.setContent(content);
				test.setChooseA(chooseA);
				test.setChooseB(chooseB);
				test.setChooseC(chooseC);
				test.setChooseD(chooseD);
				test.setAnswer(answerString.trim());				
			}
						
			Integer flag = testService.editTest(test);
			if(flag == 1){
				resultVO.setSuccess(true);
			}else{
				resultVO.setSuccess(false);
			}
		} catch (Exception e) {
			
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
		
	}

	@ResponseBody
	@RequestMapping(value="/deleteTest")
	public CommonMessageResultVO deleteTest(Integer testId){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String ,String> messageMap = new HashMap<String ,String>();
		try {
		 Integer flag = testService.deleteTest(testId);
			if(flag == 1){
				resultVO.setSuccess(true);
			}else{
				resultVO.setSuccess(false);
			}
		} catch (Exception e) {
			
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;		
	}
	
	@RequestMapping(value="/decorator")
	public String showDecorator(Model model){		
		return "index/index";
	}
	
	@RequestMapping(value="/changeLanguage")
	public String changeLocale(HttpServletRequest request , HttpServletResponse response , String localeType){
		if("en_US".equals(localeType)){ 
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(localeType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/testList";
	}
	
  
}
